package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.entity.User;
import org.example.exception.ErrorEnum;
import org.example.exception.ServiceException;
import org.example.mapper.UserMapper;
import org.example.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Override
    public Map<String, Object> login(User user, HttpSession session) {
        // 创建条件构造器对象
        QueryWrapper queryWrapper = new QueryWrapper();
        // 指定用账号作为查询条件
        queryWrapper.eq("account", user.getAccount());
        // 调用selectOne方法查出账户
        User one = userMapper.selectOne(queryWrapper);
        // 判断账户是否存在
        if (one == null) {
            throw new ServiceException(ErrorEnum.ACCOUNT_NOT_EXISTS);
        }
        // 判断账户是否禁用
        if (one.getStatus() == 1) {
            throw new ServiceException(ErrorEnum.ACCOUNT_IS_DISABLED);
        }
        // 判断密码是否正确
        if (!one.getPassword().equals(user.getPassword())) {
            throw new ServiceException(ErrorEnum.PASSWORD_ERROR);
        }
        // 设置session
        session.setAttribute("user", one);
        Map map = new HashMap();
        map.put("username", one.getUsername());
        map.put("role", one.getRole());
        return success(map);
    }
}










